﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DBMS.Models.DataInterfaces;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using DBMS.Models;
using DBMS.Helpers.Application;

namespace DBMS.Data
{
    public class RelationshipRepository : IRelationshipRepository
    {

        private void SetCurrentDirectoryToAppDir()
        {
            string appPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
            Directory.SetCurrentDirectory(appPath);
        }

        public Relationship GetRelationship(int databaseId, int relationshipId)
        {
            SetCurrentDirectoryToAppDir();

            Stream stream = File.Open("databases//" + databaseId.ToString() + "//R" + relationshipId.ToString(), FileMode.Open);
            BinaryFormatter bformatter = new BinaryFormatter();
            Relationship relationship = (Relationship)bformatter.Deserialize(stream);
            stream.Close();

            return relationship;
        }

        public IList<Relationship> GetAllRelationshipsForDatabase(int databaseId)
        {
            SetCurrentDirectoryToAppDir();

            IList<Relationship> relationships = new List<Relationship>();
            string[] relationshipFiles = Directory.GetFiles("databases//" + databaseId.ToString());

            ValueRepository valueRepo = new ValueRepository();

            TableRepository tableRepository = new TableRepository();
            ColumnRepository columnRepository = new ColumnRepository();

            foreach (string relationshipFile in relationshipFiles)
            {
                if (relationshipFile.Contains("R"))
                {
                    Stream stream = File.Open(relationshipFile, FileMode.Open);
                    BinaryFormatter bformatter = new BinaryFormatter();
                    Relationship relationship = (Relationship)bformatter.Deserialize(stream);
                    stream.Close();

                    if (!relationship.Deleted)
                    {
                        relationship.PrimaryTable = tableRepository.GetTable(databaseId, relationship.PrimaryTableId);
                        relationship.PrimaryColumn = columnRepository.GetColumn(databaseId, relationship.PrimaryTableId, relationship.PrimaryColumnId);
                        relationship.ForeignTable = tableRepository.GetTable(databaseId, relationship.ForeignTableId);
                        relationship.ForeignColumn = columnRepository.GetColumn(databaseId, relationship.ForeignTableId, relationship.ForeignColumnId);
                        relationships.Add(relationship);
                    }
                }
            }

            return relationships;
        }


        public void Save(int databaseId, Relationship relationship)
        {
            SetCurrentDirectoryToAppDir();

            string[] temp = Directory.GetFiles(Configuration.Instance.GetDatabaseFolder(databaseId));

            int relationshipIndex = temp.Length + 1;
            relationship.Index = relationshipIndex;

            string colPath = "databases//" + databaseId + "//R" + relationship.Index;

            Stream stream = File.Open(colPath, FileMode.OpenOrCreate);
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(stream, relationship);
            stream.Close();
        }

        public void Update(int databaseId, Relationship relationship)
        {
            throw new NotImplementedException();
        }

        public void Delete(int databaseId, int relationshipId)
        {
            IList<Relationship> relationships = new List<Relationship>();
            string[] relationshipFiles = Directory.GetFiles("databases//" + databaseId.ToString());

            foreach (string relationshipFile in relationshipFiles)
            {
                if (relationshipFile.Contains("R"))
                {
                    Stream stream = File.Open(relationshipFile, FileMode.Open);
                    BinaryFormatter bformatter = new BinaryFormatter();
                    Relationship relationship = (Relationship)bformatter.Deserialize(stream);
                    stream.Close();

                    relationships.Add(relationship);
                }
            }

            int temp = relationshipId;

            foreach (Relationship relationship in relationships)
            {
                if (relationship.Index == temp)
                {
                    relationship.Deleted = true;

                    string colPath = "databases//" + databaseId + "//R" + relationship.Index;

                    Stream stream = File.Open(colPath, FileMode.Truncate);
                    BinaryFormatter bformatter = new BinaryFormatter();
                    bformatter.Serialize(stream, relationship);
                    stream.Close();
                }


            }
        }
    }
}